﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Data.SqlClient;
using RTSoft.SoureCode.Control.Login;
using RTSoft.SoureCode.Control.NhanVien;
using RTSoft.Modules;
using System.Configuration;
using System.Xml;


namespace RTSoft.SoureCode.Entity
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        public frmLogin()
        {
            InitializeComponent();
        }
        
        String errConnect = "";
        String errSQL = "";


        private void TestConnection()
        {
            string DataSource = txt_server.Text.Trim();
            string Catalog = txt_database.Text.Trim();
            string UserID = txt_userid.Text.Trim();
            string Password = txt_password.Text.Trim();
            string conn = "Data Source=" + DataSource + ";Initial Catalog=" + Catalog + ";User ID=" + UserID + ";Password=" + Password;
            //string conn = "Data Source=" + txtMayChu.Text.Trim() +";Initial Catalog=" + txtTenCSDL.Text.Trim() + ";User ID=" + txtTenTaiKhoan.Text.Trim() + ";Password=" + txtMatMa.Text.Trim();
            try
            {
                //DataClasses1DataContext db = new DataClasses1DataContext(conn);
                SqlConnection sql = new SqlConnection(conn);
                sql.Open();
                sql.Close();
                XtraMessageBox.Show("Kết nối thành công");
                //SystemInfo.pub_ConnString = conn;
            }
            catch (Exception)
            {
                XtraMessageBox.Show("Kết nối bị lỗi xin thử lại");
                return;
            }
        }

    

        private void frmLogin_Load(object sender, EventArgs e)
        {
            txtUsername.Text = "Admin";
            txt_server.Text = ConfigurationManager.AppSettings.Get("ServerName");
            txt_database.Text = ConfigurationManager.AppSettings.Get("Database");
            txt_userid.Text = ConfigurationManager.AppSettings.Get("UserID");
            txt_password.Text = ConfigurationManager.AppSettings.Get("Password");
        }

        private void btn_logindetail_Click(object sender, EventArgs e)
        {
            Int32 i = btn_logindetail.ImageIndex;
            i = i == 1 ? 2 : 1;
            btn_logindetail.ImageIndex = i;
            panel_dbinfo.Visible = panel_dbinfo.Visible == true ? false : true;
            Int32 h = i == 1 ? 320 : 170;
            this.Size = new Size(this.Size.Width, h);
        }

        private void btn_test_Click(object sender, EventArgs e)
        {
            TestConnection();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {


            StringBuilder Con = new StringBuilder("Data Source=");
            Con.Append(txt_server.Text);
            Con.Append(";Initial Catalog=");
            Con.Append(txt_database.Text);
            if (String.IsNullOrEmpty(txt_userid.Text) && String.IsNullOrEmpty(txt_password.Text))
                Con.Append(";Integrated Security=true;");
            else
            {
                Con.Append(";User Id=");
                Con.Append(txt_userid.Text);
                Con.Append(";Password=");
                Con.Append(txt_password.Text);
            }
            string strCon = Con.ToString();
            //Set giá trị cho chuỗi connectString trong app.config
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            connectionStringsSection.ConnectionStrings["sqlConnect"].ConnectionString = strCon;
            config.Save();
            ConfigurationManager.RefreshSection("connectionStrings");
           
            //Set giá trị cho các biến trong app.config
            ConfigurationManager.AppSettings.Set("ServerName", txt_server.Text);
            ConfigurationManager.AppSettings.Set("Database", txt_database.Text);
            ConfigurationManager.AppSettings.Set("UserID", txt_userid.Text);
            ConfigurationManager.AppSettings.Set("Password", txt_password.Text);
            btn_logindetail_Click(null, null);
            ConfigurationManager.RefreshSection("connectionStrings");
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            loginctrl ctrl = new loginctrl();
            int rs = ctrl.isValidate(ref errConnect, ref errSQL, txtUsername.Text, txtPassword.Text);
            if (rs != 0)
            {
                nhanvieninfo nv = new nhanvieninfo();
                nv = ctrl.getEmployee(ref errConnect, ref errSQL, txtUsername.Text);
                RTValidate.RTUser = nv.TEN_NV;
                RTValidate.RTJobTittle = nv.ID_NHOM;
                frmMain main = new frmMain();
                this.Hide();
                main.ShowDialog();
            }
            else
            {
                Library.ShowMessage("Kiểm tra mật khẩu!");
            }
        }

        //public void updateConfigFile(string con)
        //{
        //    //updating config file
        //    XmlDocument XmlDoc = new XmlDocument();
        //    //Loading the Config file
        //    XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        //    // XmlDoc.Load("App.config");
        //    foreach (XmlElement xElement in XmlDoc.DocumentElement)
        //    {
        //        if (xElement.Name == "connectionStrings")
        //        {
        //            //setting the coonection string
        //            xElement.FirstChild.Attributes[2].Value = con;
        //        }
        //    }
        //    //writing the connection string in config file
        //    XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        //    //XmlDoc.Save("App.config");
        //}

        
    }
}